import json
import random
import re

from celery.app import shared_task
from django.db.models.expressions import result
from django.shortcuts import render, redirect

# Create your views here.
from rest_framework.views import APIView
from rest_framework.response import Response
from user import models,userser
from django.db.models import Q
from utils.jwttool import tooljwt

from utils.jwttool import tooljwt
from utils.myredis import redistool
from utils.send import send_message


class LoginByPsw(APIView):
    def post(self,request):
        account=request.data.get('account')
        password=request.data.get('password')
        query=Q(username=account)|Q(phone=account)|Q(email=account)
        info=models.UserModel.objects.filter(query).first()
        if not info:
            return Response({"msg":"账号不存在","code":400})
        if (info.password==password):
            token=tooljwt.get_token(info.id)
            return Response({"msg":"登录成功","code":200,"data":{"token":token,"userid":info.id}})
        else:
            return Response({"msg":"密码错误","code":400})

# class SendCode(APIView):
#     def get(self, request):
#         phone=request.query_params.get('phone')
#         result=re.compile(r'^1[3-9]\d{9}$')
#         if not result.match(phone):
#             return Response({"code":400, "msg": "手机号格式不正确"})
#         # 发送验证码
#         code=str(random.randint(1000,9999))
#         res=send_message(phone,code)
#         resjson=json.loads(res)
#         if resjson['statusCode']=="000000":
#             redistool.set_code(phone, code)
#             return Response({'code':200,'msg':'发送成功'})
#         else:
#             return Response({'code':400,'msg':'发送失败'})



from .task import sendmsg
from utils.myredis import  redistool
#发送验证码
class SendCode(APIView):
    def get(self,request):
        phone=request.query_params.get("phone")
        #写一个正则验证手机号的合法性
        result=re.compile(r"^1[3456789]\d{9}$")
        if not result.match(phone):#如果手机号不合法
            return Response({"msg":"手机号不合法","code":400})
        #手机号合法 发送验证码
        code=str(random.randint(1000,9999))
        #调用发送验证码的函数
        #res=send_message(phone,code)
        #调用异步函数
        task=sendmsg.delay(phone,code)#异步任务的调用
        #拿到异步任务的结果
        res=task.get()
        #将res为json格式
        resjson=json.loads(res)
        if resjson["statusCode"]=="000000":
            redistool.setcode(phone,code)
            #发送成功后将code存储到redis中
            return Response({"msg":"发送成功","code":200})
        else:
            return Response({"msg":"发送失败","code":400})

class LoginByCode(APIView):
    def post(self,request):
        phone=request.data.get('phone')
        code=request.data.get('code')
        result=re.compile(r'^1[3-9]\d{9}$')
        if not result.match(phone):
            return Response({"code":400, "msg": "手机号格式不正确"})
        if redistool.get_code(phone)!=code:
            return Response({"code":400, "msg": "验证码错误"})
        user=models.UserModel.objects.filter(phone=phone).first()
        if not user:
            user=models.UserModel.objects.create(phone=phone)
        token=tooljwt.get_token(user.id)
        return Response({"msg":"登录成功","code":200,"data":{"token":token,"userid":user.id}})

# class Register(APIView):
#     def post(self,request):
#         phone=request.data.get('phone')
#         password=request.data.get('password')
#         re_password = request.data.get('re_password')
#         code=request.data.get('code')
#         rediscode=redistool.get_code(phone)
#         if code!=rediscode:
#             return Response({"msg":"验证码错误","code":400})
#         user=models.UserModel.objects.filter(phone=phone).first()
#         if not user:
#             user=models.UserModel.objects.create(phone=phone,password=password)
#         token=tooljwt.get_token(user.id)
#         return Response({"msg":"注册成功","code":200,"data":{"token":token,"userid":user.id}})

# class Register(APIView):
#     def post(self,request):
#         phone=request.data.get("account")
#         password=request.data.get("password")
#         code=request.data.get("code")
#         rediscode=redistool.getcode(phone)
#         if code != rediscode:
#             return Response({"msg":"验证码错误","code":400})
#         user=models.UserModel .objects.filter(phone=phone).first()
#         if not user:
#             user=models.UserModel.objects.create(phone=phone,password=password)
#         token=tooljwt.get_token(user.id)
#         return Response({"msg":"注册成功","code":200,"data":{"token":token,"userid":user.id}})


class Register(APIView):
    def post(self, request):
        phone=request.data.get('phone')
        password=request.data.get('password')
        re_password=request.data.get('re_password')
        code=request.data.get('code')
        result = re.compile(r'^1[3-9]\d{9}$')
        if not result.match(phone):
            return Response({"code": 400, "msg": "手机号格式不正确"})
        if password!=re_password:
            return Response({'code':400,'msg':'两次密码不一致'})
        rediscode=redistool.get_code(phone)
        if code!=rediscode:
            return Response({'code':400,'msg':'验证码错误'})
        user=models.UserModel.objects.filter(phone=phone).first()
        if user:
            return Response({'code':400,'msg':'用户已存在'})
        user=models.UserModel.objects.create(phone=phone,password=password)
        token=tooljwt.get_token(user.id)
        return Response({'code':200,'msg':'注册成功',"data":{"token":token,"userid":user.id}})

from .task import sendmessage
class TestView(APIView):
    def get(self,request):
        #调用任务
        sendmessage.delay("小三","下大雨")
        return Response({"msg":"测试成功","code":200})

from utils.send import send_message
#新建异步任务
@shared_task(name='send')
def sendmsg(phone,code):
    #就是管着发短信a
    return send_message(phone,code)